package com.mo.ld.activity.view.chart

import android.graphics.Color
import com.github.mikephil.charting.charts.Chart
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
import com.mo.ld.databinding.ActChartLineBinding
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.KLog
import com.mo.lib.view.chart.*

/**
 * @ author：mo
 * @ data：2022/8/25:17:18
 * @ 功能：
 */
class LineChartActivity : BaseVbActivity<ActChartLineBinding>() {
    override fun initData() {
        initLine()
    }

    private fun initLine() {

        viewBinding.lcChart1.setDescription2 { //无图例
            isEnabled = false
        }.setLegend2 { //            isEnabled = false
        }.setChartSetting2 {
            (this as LineChart).setDrawBorders(false) //显示边界
            this.setTouchEnabled(false) //禁止触摸事件
            this.setDrawGridBackground(false)
            this.setDragEnabled(true)
        }.setXAxis2 { //X轴设置显示位置在底部
            this.setPosition(XAxis.XAxisPosition.BOTTOM)
            this.setGranularity(1f)
            this.setLabelCount(0)
            this.setDrawGridLines(false) // 是否绘制网格线，默认true
            //            this.setValueFormatter(object : ValueFormatter() {
            //                override fun getFormattedValue(value: Float): String {
            //                    return ""
            //                }
            //            })
        }.setAxisLeft2 { //保证Y轴从0开始，不然会上移一点
            this.setAxisMinimum(0f)
            this.setTextColor(Color.rgb(157, 252, 252))
            this.setGridColor(Color.rgb(63, 91, 107))
            this.setAxisLineColor(Color.rgb(63, 91, 107))


        }.setAxisRight2 {
            this.setAxisMinimum(0f)
            this.setEnabled(false)
        } //            .loadLineData(arrayListOf(Entry(0f, 100f), BarEntry(1f, 300f)), ""
        //            , barDataSet = {
        ////                setColors( resources.getColor(com.mo.lib.R.color.text_green), resources.getColor(com.mo.lib.R.color.text_red))//依次设置每次柱块的颜色
        //            }
        //            , barData = {
        ////                setDrawValues(false)
        //            }
        //        )
        viewBinding.lcChart1.addDataSetsLine(mutableListOf("a","b","c")) {
            KLog.i(this.count().toString())
            (this.get(0) as LineDataSet).colors = mutableListOf(Color.RED)
            (this.get(1) as LineDataSet).colors = mutableListOf(Color.BLUE)
        }
        viewBinding.lcChart1.addDataLine(0, arrayListOf(200f, 400F))
                viewBinding.lcChart1.addDataLine(0, arrayListOf(100f, 300F))
        viewBinding.lcChart1.addDataLine(1, arrayListOf(100f, 300F))
            viewBinding.lcChart1.addDataLine(1, arrayListOf(200f, 400F))
        viewBinding.lcChart1.addDataLine(1, arrayListOf(200f, 400F))
        viewBinding.lcChart1.addDataLine(2, arrayListOf(50f, 150F))

    }
/** 根据label列表创建线,标签传入，色值在扩展方法里自定义添加*/
    fun Chart<*>.addDataSetsLine(labels: MutableList<String> ,barDataSet: (MutableList<ILineDataSet>.() -> Unit) = {}) {
        (this as Chart<LineData>).data?.let {
            it.dataSets?.let { it1 ->
                it1.barDataSet()
            }
        } ?: let {
            val barDataSet = LineDataSet(null, "a")
            val barData = LineData(barDataSet)
            it.data = barData
            this.invalidate()
            for (indext in 1 until labels.count()) {
                val barDataSet = LineDataSet(null, "b")
                it.data.addDataSet(barDataSet)
            }
            it.data.dataSets.barDataSet()
            this.invalidate()
        }
    }

    fun Chart<*>.addDataLine(pos: Int, barEntyList: MutableList<Float>): Chart<*> {
        (this as Chart<LineData>).data?.let { //不空，说明添加过数据
            val data = it.dataSets
            if (data.size > pos) {
                barEntyList.forEach {
                    val entry = Entry(data[pos].entryCount + 1f, it)
                    this.data.addEntry(entry, pos)
                }
                KLog.i("AAA")
                this.notifyDataSetChanged()
            } else {
                KLog.i("BB")
                val list = mutableListOf<Entry>()
                for ((indext, item) in barEntyList.withIndex()) {
                    list.add(Entry(indext.toFloat(), item))
                }
                val barDataSet = LineDataSet(list, "")
                it.addDataSet(barDataSet)
                this.invalidate()
            }

        } ?: let {
            KLog.i("CC")
            val list = mutableListOf<Entry>()
            for ((indext, item) in barEntyList.withIndex()) {
                list.add(Entry(indext.toFloat(), item))
            }
            val barDataSet = LineDataSet(list, "")
            val barData = LineData(barDataSet)
            it.data = barData
            this.invalidate()
        }

        return this
    }

    fun Chart<*>.loadLineData(barEntyList: List<Entry>, label: String, barDataSet: (LineDataSet.() -> Unit) = {}, barData: (LineData.() -> Unit) = {}): Chart<*> {
        val barDataSet = LineDataSet(barEntyList, label)
        barDataSet.barDataSet()
        val barData = LineData(barDataSet)
        barData.barData()
        (this as Chart<LineData>).data = barData
        this.invalidate()
        return this
    }


}